/**
 * chenuu.com
 * 
 * @author  zhaoxu.com@gmail.com
 * @created 2012-11-10
 */
package com.chenuu.integration.dropbox.session;

import org.apache.http.client.HttpClient;

import com.chenuu.common.config.PropertiesConfigFactory;
import com.chenuu.integration.dropbox.config.DropboxConfig;
import com.chenuu.integration.httpclient.oauth.OAuthToken;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.Session.AccessType;
import com.dropbox.client2.session.WebAuthSession;

/**
 *
 */
public class DefaultDropboxSession implements DropboxSession {
	
	private static DropboxConfig config = PropertiesConfigFactory.getConfig(DropboxConfig.class);

	private HttpClient httpClient;
	private OAuthToken oAuthToken;
	
	public DefaultDropboxSession() {
	}
	
	public DefaultDropboxSession(String key, String secret) {
		oAuthToken = new OAuthToken(key, secret);
	}

	@Override
	public HttpClient getHttpClient() {

		if (httpClient == null) {
			AppKeyPair appKeyPair = new AppKeyPair(config.getAppKey(), config.getAppSecret());
			WebAuthSession was = new WebAuthSession(appKeyPair , AccessType.APP_FOLDER);
			httpClient = was.getHttpClient();
		}

		return httpClient;
	}

	@Override
	public OAuthToken getOAuthToken() {
		return oAuthToken;
	}

	@Override
	public void setOAuthToken(OAuthToken oAuthToken) {
		this.oAuthToken = oAuthToken;
	}

}
